package com.cn.xiaonuo.tcrfinance.tcrshops.controller;

import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.ApiJsonModel;
import com.cn.xiaonuo.core.annotion.BusinessLog;
import com.cn.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.PvProductGroupParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.service.ProductGroupService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * banner图
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
public class ProductGroupController {


    @Autowired
    private ProductGroupService productGroupService;


    /**
     * 查询免税分组列表
     */
    @ApiJsonModel({
            @ApiModelProperty(name = "name", value = "分组名称",example = "分组名称"),
    })
    @ApiOperation(value = "分组列表", notes = "分组列表")
    @PostMapping("/api/productGroup/list")
    public BusinessMessage list(@RequestBody PvProductGroupParam productGroupParam){

        return productGroupService.list(productGroupParam);
    }


    /**
     * 查询分组
     *
     * @author luyangyang
     * @date 2021-10-21 16:14:37
     */
    //@Permission
    /**
     * 查询免税分组列表
     */
    @ApiJsonModel({
            @ApiModelProperty(name = "name", value = "分组名称",example = "分组名称"),
    })
    @PostMapping("/api/productGroup/page")
    @BusinessLog(title = "分组_分页", opType = LogAnnotionOpTypeEnum.QUERY)
    @ApiOperation(value = "分组_分页", notes = "分组_分页")
    public ResponseData page(@RequestBody PvProductGroupParam productGroupParam) {
        return new SuccessResponseData(productGroupService.page(productGroupParam));
    }
    /**
     * 添加分组
     *
     * @author luyangyang
     * @date 2021-10-21 16:14:37
     */
    //@Permission
    @PostMapping("/api/productGroup/add")
    @BusinessLog(title = "分组_增加", opType = LogAnnotionOpTypeEnum.ADD)
    @ApiOperation(value = "分组_增加", notes = "分组_增加")
    public ResponseData add(@RequestBody @Validated(PvProductGroupParam.add.class) PvProductGroupParam productGroupParam) {
        SuccessResponseData responseData= new SuccessResponseData();
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        productGroupParam.setCreatedAt(currentTime);
        productGroupParam.setUpdatedAt(new Date());
        if(StringUtils.isBlank(productGroupParam.getName())){
            responseData.setSuccess(false);
            responseData.setCode(1);
            responseData.setMessage("类型名称不能为空");
            return responseData;
        }
        if(StringUtils.isBlank(productGroupParam.getImageUrl())){
            responseData.setSuccess(false);
            responseData.setCode(1);
            responseData.setMessage("类型图片不能为空");
            return responseData;
        }
        productGroupService.add(productGroupParam);
        return new SuccessResponseData();
    }

    /**
     * 删除分组
     *
     * @author luyangyang
     * @date 2021-10-21 16:14:37
     */
    // @Permission
    @PostMapping("/api/productGroup/delete")
    @BusinessLog(title = "分组_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    @ApiOperation(value = "分组_删除", notes = "分组_删除")
    public ResponseData delete(@RequestBody @Validated(PvProductGroupParam.delete.class) PvProductGroupParam productGroupParam) {
        productGroupService.delete(productGroupParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑分组
     *
     * @author luyangyang
     * @date 2021-10-21 16:14:37
     */
    //@Permission
    @PostMapping("/api/productGroup/edit")
    @BusinessLog(title = "分组_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    @ApiOperation(value = "分组_编辑", notes = "分组_编辑")
    public ResponseData edit(@RequestBody @Validated(PvProductGroupParam.edit.class) PvProductGroupParam productGroupParam) {
        productGroupParam.setUpdatedAt(new Date());
        productGroupService.edit(productGroupParam);
        return new SuccessResponseData();
    }

    /**
     * 查看分组
     *
     * @author luyangyang
     * @date 2021-10-21 16:14:37
     */
    //@Permission
    @PostMapping("/api/productGroup/detail")
    @BusinessLog(title = "商品分组_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    @ApiJsonModel({
            @ApiModelProperty(name = "id", value = "商品分组id",example = "商品分组id",required = true),
    })
    @ApiOperation(value = "商品分组_查看", notes = "商品分组_查看")
    public ResponseData detail(@RequestBody PvProductGroupParam productGroupParam) {
        return new SuccessResponseData(productGroupService.detail(productGroupParam));
    }

    /**
     * 禁用商品分类
     * @param productGroupParam
     * @return
     */
    @ApiJsonModel({
            @ApiModelProperty(name = "id", value = "分组id",example = "分组id"),
            @ApiModelProperty(name = "del", value = "状态",example = "状态"),
    })
    @ApiOperation(value = "分组_启用/禁用", notes = "分组_启用/禁用")
    @PostMapping("/api/productGroup/updateStatus")
    public ResponseData updateStatus(@RequestBody PvProductGroupParam productGroupParam){
       productGroupService.updateStatus(productGroupParam);
        return new SuccessResponseData();
    }
}
